/**
 * 
 */
package com.renmai.contact;

import com.renmai.R;

import android.app.Activity;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * @author Issac
 *
 */
public class NewContactActivity extends Activity {

	private Button saveContact;
	private EditText name;
	private EditText phone;
	private EditText emai;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.newcontact);
		
		name = (EditText)findViewById(R.id.widget33);
		phone = (EditText)findViewById(R.id.widget45);
		emai = (EditText)findViewById(R.id.widget46);
		
		saveContact = (Button)findViewById(R.id.widget35);
		saveContact.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				save();
				Toast.makeText(NewContactActivity.this, "添加成功", Toast.LENGTH_LONG).show(); 
				Intent intent = new Intent(NewContactActivity.this, ViewContactListActivity.class);
				startActivity(intent);
				
				int version = Integer.valueOf(android.os.Build.VERSION.SDK);
				if(version  >= 5) {     
				     overridePendingTransition(R.layout.zoomin, R.layout.zoomout);  //此为自定义的动画效果，下面两个为系统的动画效果  
				     //overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);    
				     //overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);  
				}  
			}
			
		});
		
		
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	public void save(){
		
		//save in phone
		ContentValues values = new ContentValues();  
		Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
		//获取id  
        long rawContactId = ContentUris.parseId(rawContactUri);  
        //往data表入姓名数据  
        values.clear();  
        values.put(Data.RAW_CONTACT_ID, rawContactId); //添加id  
        values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);//添加内容类型（MIMETYPE）  
        values.put(StructuredName.GIVEN_NAME, name.getText().toString());//添加名字，添加到first name位置  
        this.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);  
        
        //往data表入电话数据  
        values.clear();  
        values.put(Data.RAW_CONTACT_ID, rawContactId);  
        values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);  
        values.put(Phone.NUMBER, phone.getText().toString());  
        values.put(Phone.TYPE, Phone.TYPE_MOBILE);  
        this.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);  
        
        //往data表入Email数据  
        values.clear();  
        values.put(Data.RAW_CONTACT_ID, rawContactId);  
        values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);  
        values.put(Email.DATA, emai.getText().toString());  
        values.put(Email.TYPE, Email.TYPE_WORK);  
        this.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);  
		
        
        //save in sim card
        values.clear();
        values.put("tag", name.getText().toString());
        values.put("number", phone.getText().toString());
        getContentResolver().insert(Uri.parse("content://icc/adn"), values);
        
        
	}
	
}
